package com.example.controller.seller;

import com.example.base.BaseController;
import com.example.dto.OrderSearchDto;
import com.example.dto.ReturnApplyDto;
import com.example.entity.Order;
import com.example.service.OrderReturnApplyService;
import com.example.service.OrderService;
import com.example.vo.IdHolder;
import com.example.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsc
 * @date 2024/6/10
 */
@RestController()
@Slf4j
@RequestMapping("/seller/order")
public class SellerOrderController extends BaseController<OrderService, Order> {
    @Resource
    private OrderService orderService;

    @Autowired
    private OrderReturnApplyService orderReturnApplyService;

    @GetMapping("/getList")
    public Result<List<Order>> getOrderList(@RequestParam("storeId") Long storeId){
        return Result.ok(orderService.getSellerOrderList(storeId));
    }

    @GetMapping("/getReturn")
    @Operation(summary = "获取售后信息")
    public Result<List<ReturnApplyDto>> getReturnList(){
        Long storeId = IdHolder.getStoreId();
        return orderReturnApplyService.getReturnListByStoreId(storeId);
    }

    @PostMapping("/searchReturn")
    @Operation(summary = "查询售后信息")
    public Result<List<ReturnApplyDto>> searchReturn(@RequestBody OrderSearchDto orderSearchDto){
        Long storeId = IdHolder.getStoreId();
        return orderReturnApplyService.searchReturnByStoreId(storeId,orderSearchDto);
    }

    @PostMapping("/handle")
    @Operation(summary = "查询售后信息")
    public Result<String> decision(@RequestParam Long id, @RequestParam String handleNote, @RequestParam String decisionType){
        int status = "agree".equals(decisionType) ? 5 : "reject".equals(decisionType) ? 3 : -1;
        if(status == -1){
            Result.error("操作不正确");
        }
        boolean isUpdate = orderReturnApplyService.updateOrderReturn(id,handleNote,status);
        if(isUpdate){
            return Result.ok("处理成功");
        }
        return Result.error("该订单已处理");
    }
}
